import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:paly_android/api/api.dart';
import 'package:paly_android/base/base_widget.dart';
import 'package:paly_android/net/dio_client.dart';
import 'package:paly_android/ui/toast_no_context.dart';
import 'package:paly_android/util/toast_util.dart';

class RegisterPage extends BaseWidget {
  RegisterPage({Key? key}) : super(key: key);

  @override
  BaseWidgetState<BaseWidget> getState() {
    return _RegisterPageState();
  }
}

class _RegisterPageState extends BaseWidgetState<RegisterPage> {
  TextEditingController _userNameController = TextEditingController();
  TextEditingController _pwdController = TextEditingController();

  @override
  void initState() {
    super.initState();
    showBackLeading=true;
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget baseBuild(BuildContext context) {
    return Form(
        autovalidateMode: AutovalidateMode.always, //开启自动校验
        child: Padding(
          padding: EdgeInsets.all(10),
          child: Column(
            children: [
              TextFormField(
                controller: _userNameController,
                decoration:
                InputDecoration(label: Text("用户名"), hintText: "请输入手机号"),
                keyboardType: TextInputType.phone,
              ),
              TextFormField(
                controller: _pwdController,
                obscureText: true,
                decoration:
                InputDecoration(label: Text("密码"), hintText: "请输入密码"),
                validator: (value) {
                  if (value!.isNotEmpty && value.length < 6) {
                    return "密码最少为6位";
                  }
                },
              ),
              Padding(
                padding: EdgeInsets.only(top: 30),
                child: Row(
                  children: [
                    Expanded(
                        child: ElevatedButton(
                          // style: ButtonStyle(
                          //   padding: MaterialStateProperty.all(EdgeInsets.all(30))
                          // ),
                          child: Text("注册"),

                          onPressed: () async {
                            String user = _userNameController.text;
                            String pwd = _pwdController.text;
                            if (user.isEmpty || pwd.isEmpty) {
                              showShortToast("账号或密码不能为空");
                              return;
                            }
                            LogUtil.e("user: $user   pwd:$pwd");

                            //  13254112365
                            Map data= await DioClient.instance.post(Api.REGISTER,parameters: {"username":user,"password":pwd,"repassword":pwd});
                            if(data!=null){
                              LogUtil.e("object ${data.toString()}");
                              showLongToast("注册成功,请到登录页面登录");
                              Navigator.of(context).pop();
                            }

                            // Navigator.of(context).push(MaterialPageRoute(builder: (context)=>ToastNoContext(),));

                          },
                        ))
                  ],
                ),
              )
            ],
          ),
        ));
  }

  @override
  String getAppBarTitle() => "注册";
}
